<template>
  <div v-if="visible" class="toast" :class="type" @click="handleClick">
    <div class="toast-content">
      <div class="toast-left">
        <svg v-if="type === 'success'" class="toast-icon" width="20" height="20" viewBox="0 0 24 24" fill="currentColor">
          <path d="M12,2A10,10 0 0,1 22,12A10,10 0 0,1 12,22A10,10 0 0,1 2,12A10,10 0 0,1 12,2M11,16.5L18,9.5L16.59,8.09L11,13.67L7.41,10.09L6,11.5L11,16.5Z"/>
        </svg>
        <svg v-else-if="type === 'error'" class="toast-icon" width="20" height="20" viewBox="0 0 24 24" fill="currentColor">
          <path d="M12,2A10,10 0 0,1 22,12A10,10 0 0,1 12,22A10,10 0 0,1 2,12A10,10 0 0,1 12,2M12,7A1,1 0 0,0 11,8V12A1,1 0 0,0 12,13A1,1 0 0,0 13,12V8A1,1 0 0,0 12,7M12,17A1,1 0 0,1 11,16A1,1 0 0,1 12,15A1,1 0 0,1 13,16A1,1 0 0,1 12,17Z"/>
        </svg>
        <svg v-else class="toast-icon" width="20" height="20" viewBox="0 0 24 24" fill="currentColor">
          <path d="M12,2A10,10 0 0,1 22,12A10,10 0 0,1 12,22A10,10 0 0,1 2,12A10,10 0 0,1 12,2M12,7A1,1 0 0,0 11,8V12A1,1 0 0,0 12,13A1,1 0 0,0 13,12V8A1,1 0 0,0 12,7M12,17A1,1 0 0,1 11,16A1,1 0 0,1 12,15A1,1 0 0,1 13,16A1,1 0 0,1 12,17Z"/>
        </svg>
        <span class="toast-message">{{ message }}</span>
      </div>
      <button @click.stop="$emit('close')" class="toast-close">
        <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
          <path d="M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z"/>
        </svg>
      </button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Toast',
  props: {
    message: {
      type: String,
      required: true
    },
    type: {
      type: String,
      default: 'info',
      validator: value => ['success', 'error', 'warning', 'info'].includes(value)
    },
    duration: {
      type: Number,
      default: 3000
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      timer: null
    }
  },
  watch: {
    visible(newVal) {
      // 清除之前的定时器
      if (this.timer) {
        clearTimeout(this.timer)
        this.timer = null
      }
      
      // 如果显示Toast，设置自动关闭定时器
      if (newVal) {
        this.timer = setTimeout(() => {
          this.$emit('close')
          this.timer = null
        }, this.duration)
      }
    }
  },
  beforeUnmount() {
    // 组件销毁前清除定时器
    if (this.timer) {
      clearTimeout(this.timer)
      this.timer = null
    }
  },
  methods: {
    handleClick() {
      // 实现点击关闭的逻辑
      this.$emit('close')
    }
  }
}
</script>

<style scoped>
.toast {
  position: fixed;
  top: 80px;
  right: 24px;
  z-index: 20000;
  min-width: 300px;
  max-width: 500px;
  border-radius: 8px;
  padding: 16px 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(10px);
  animation: slideIn 0.3s ease-out;
}

.toast.success {
  background: rgba(29, 185, 84, 0.9);
  border: 1px solid #1DB954;
  color: white;
}

.toast.error {
  background: rgba(226, 33, 52, 0.9);
  border: 1px solid #e22134;
  color: white;
}

.toast.warning {
  background: rgba(255, 193, 7, 0.9);
  border: 1px solid #ffc107;
  color: #000;
}

.toast.info {
  background: rgba(33, 150, 243, 0.9);
  border: 1px solid #2196f3;
  color: white;
}

.toast-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
}

.toast-left {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
}

.toast-icon {
  flex-shrink: 0;
}

.toast-message {
  font-size: 14px;
  font-weight: 500;
  line-height: 1.4;
  flex: 1;
}

.toast-close {
  background: none;
  border: none;
  padding: 6px;
  cursor: pointer;
  color: white;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-left: 12px;
  flex-shrink: 0;
  transition: all 0.2s ease;
  width: 28px;
  height: 28px;
}

.toast-close:hover {
  background: rgba(0, 0, 0, 0.1);
  transform: scale(1.1);
}

@keyframes slideIn {
  from {
    transform: translateX(100%);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}
</style> 